package com.pug.web.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.multipart.MultipartException;
import xq.pug.common.exception.DefaultException;
import xq.pug.common.exception.errorType.SystemErrorType;
import xq.pug.common.result.R;

/**
 * @author RenGaoshuai
 * @date 2023/7/6 14:15
 * @description 全局异常拦截处理
 */
@Slf4j
public class DefaultGlobalExceptionHandlerAdvice {

    /**
     * servlet请求异常
     * @param ex
     * @return
     */
    @ExceptionHandler(value = {MissingServletRequestParameterException.class})
    public R<Void> missingServletRequestParameterException(MissingServletRequestParameterException ex) {
        log.error("missing servlet request parameter exception:{}", ex.getMessage());
        return R.ofFail(SystemErrorType.ARGUMENT_NOT_VALID);
    }

    /**
     * 文件上传异常
     * @param ex
     * @return
     */
    @ExceptionHandler(value = {MultipartException.class})
    public R<Void> uploadFileLimitException(MultipartException ex) {
        log.error("upload file size limit:{}", ex.getMessage());
        return R.ofFail(SystemErrorType.UPLOAD_FILE_SIZE_LIMIT);
    }

    /**
     * 方法参数校验异常
     * @param ex
     * @return
     */
    @ExceptionHandler(value = {MethodArgumentNotValidException.class})
    public R<String> serviceException(MethodArgumentNotValidException ex) {
        log.error("service exception:{}", ex.getMessage());
        return R.ofFail(SystemErrorType.ARGUMENT_NOT_VALID.getCode(), ex.getBindingResult().getFieldError().getDefaultMessage());
    }

    /**
     * 数据库唯一键重复异常
     * @param ex
     * @return
     */
    @ExceptionHandler(value = {DuplicateKeyException.class})
    public R<Void> duplicateKeyException(DuplicateKeyException ex) {
        log.error("primary key duplication exception:{}", ex.getMessage());
        return R.ofFail(SystemErrorType.DUPLICATE_PRIMARY_KEY);
    }

    /**
     * 空指针异常
     * @param ex
     * @return
     */
    @ExceptionHandler(value = {NullPointerException.class})
    public R<Void> nullPointerException(NullPointerException ex) {
        log.error("NullPointerException exception:{}", ex.getMessage());
        return R.ofFail(SystemErrorType.NULL_POINTER);
    }

    /**
     * 类型转化异常
     * @param ex
     * @return
     */
    @ExceptionHandler(value = {ClassCastException.class})
    public R<Void> baseException(ClassCastException ex) {
        log.error("ClassCastException exception:{}", ex.getMessage());
        return R.ofFail(SystemErrorType.CLASS_CAST);
    }

    /**
     * 自定义异常
     * @param ex
     * @return
     */
    @ExceptionHandler(value = {DefaultException.class})
    public R<Void> baseException(DefaultException ex) {
        log.error("base exception:{}", ex.getMessage());
        return R.ofFail(ex.getErrorType());
    }

    /**
     * 系统级别异常
     * @return
     */
    @ExceptionHandler(value = {Exception.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public R<Void> exception() {
        return R.ofFail();
    }

    /**
     * 系统级别异常
     * @return
     */
    @ExceptionHandler(value = {Throwable.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public R<Void> throwable() {
        return R.ofFail();
    }
}
